package br.com.moxeon.model.financialitems;

import java.text.SimpleDateFormat;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import br.com.moxeon.model.MoxeonControlledPersistentEntity;
import br.com.moxeon.model.cash.Throw;
import br.com.moxeon.model.financialaccounts.BankAccount;
import br.com.vexillum.model.IActivatedEntity;

@SuppressWarnings("serial")
@Entity
@Table(name = "financial_item")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class FinancialItem extends MoxeonControlledPersistentEntity implements IActivatedEntity {

	@OneToOne(targetEntity = Throw.class, fetch = FetchType.LAZY, mappedBy = "item")
	private Throw financialThrow;

	private Double value;
	
	

	public Double getItemValue() {
		if (this instanceof CashierCheck) {
			return ((CashierCheck) this).getValue();
		} else if (this instanceof DebitInAccount) {
			return ((DebitInAccount) this).getValue();
		}
		return null;
	}

	public String getType() {
		if (this instanceof CashierCheck) {
			return "Cheque";
		} else if (this instanceof DebitInAccount) {
			return "Débito em Conta";
		}
		return null;
	}

	public String getNumberItem() {
		if (this instanceof CashierCheck) {
			return ((CashierCheck) this).getNumber();
		} else if (this instanceof DebitInAccount) {
			if ( ((DebitInAccount) this).getAccount() instanceof BankAccount )
			{
				return Integer.toString(((BankAccount) ((DebitInAccount) this).getAccount() ).getBank().getNumber());
			}
		}
		return null;
	}

	public String getNameBank() {
		if (this instanceof CashierCheck) {
			return ((CashierCheck) this).getBankAccount().getBank().getName();
		} else if (this instanceof DebitInAccount && ( ((DebitInAccount) this).getAccount()  instanceof BankAccount) ) {
			return ((BankAccount) ((DebitInAccount) this).getAccount()).getBank().getName();
		}
		return null;
	}

	public String getDateMaturity() {
		if (this instanceof CashierCheck) {
			SimpleDateFormat fmt = new SimpleDateFormat("dd/MM/yyyy");  
			return fmt.format(((CashierCheck) this).getMaturity());
		} else if (this instanceof DebitInAccount) {
			return "";
		}
		return null;
	}

	public Double getValue() {
		return value;
	}

	public void setValue(Double value) {
		this.value = value;
	}
	
	public Throw getFinancialThrow() {
		return financialThrow;
	}

	public void setFinancialThrow(Throw financialThrow) {
		this.financialThrow = financialThrow;
	}
}